/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.social.web;

import com.nc.kyiv.yellow.sociallibrary.helper.web.CookieHelper;
import com.nc.kyiv.yellow.sociallibrary.helper.web.HtmlHelper;
import com.nc.kyiv.yellow.sociallibrary.helper.web.JsonHelper;
import com.nc.kyiv.yellow.sociallibrary.helper.web.SaveToDataBaseHelper;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author My
 */
public class Vk extends HttpServlet {

    String access_token;
    String personalData;
    final String CLIENT_ID = "4039026";
    final String CLIENT_SECRET = "wotjPCDTdKMI0TyJr5Fa";
    final String REDIRECT_URI = "http://localhost:8081/SocialLibrary-web/Vk";
    final String RESPONSE_TYPE = "code";
    final String MAIN_PAGE_URL = "http://localhost:8081/SocialLibrary-web/";


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        if (request.getParameter("code") != null) {
            StringBuilder stringUrlForAccessToken = new StringBuilder();
            stringUrlForAccessToken.append("https://oauth.vk.com/access_token?client_id=");
            stringUrlForAccessToken.append(CLIENT_ID);
            stringUrlForAccessToken.append("&client_secret=");
            stringUrlForAccessToken.append(CLIENT_SECRET);
            stringUrlForAccessToken.append("&code=");
            stringUrlForAccessToken.append(request.getParameter("code"));
            stringUrlForAccessToken.append("&redirect_uri=");
            stringUrlForAccessToken.append(REDIRECT_URI);
            URL urlForAccessToken = new URL(stringUrlForAccessToken.toString());

//            URL urlForAccressToken = new URL("https://oauth.vk.com/access_token?client_id=" + CLIENT_ID + "&"
//                    + "client_secret=" + CLIENT_SECRET + "&code=" + request.getParameter("code")
//                    + "&redirect_uri=" + REDIRECT_URI);
            access_token = (String)JsonHelper.parseJSON(HtmlHelper.getStringFromHTML(urlForAccessToken)).get("access_token");
                   
            URL urlForPersonalData = new URL("https://api.vk.com/method/users.get?fields=sex,bdate&access_token=" + access_token);
            
            personalData = HtmlHelper.getStringFromHTML(urlForPersonalData);
            
            // Save to DB
            Map parsedData = JsonHelper.parseJSON(personalData);
            SaveToDataBaseHelper.saveUser(parsedData);

            /*create cookies*/
            CookieHelper.createCookies(parsedData, response);

            response.sendRedirect(MAIN_PAGE_URL);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}